import time

import cv2
import numpy as np
import pyautogui

from utils import locate_bobber

# 加载的时候读取一次图片就够了
hook_images = [
    # (cv2.imread('hook_img/hook1.png'), 0.6),
    # (cv2.imread('hook_img/hook2.png'), 0.6),
    (cv2.imread('hook_img/hook3.png'), 0.6),
    # (cv2.imread('hook_img/hook4.png'), 0.6),
    # (cv2.imread('hook_img/hook5.png'), 0.6),
    # (cv2.imread('hook_img/hook6.png'), 0.6),
    # (cv2.imread('hook_img/hook7.png'), 0.6),
    # (cv2.imread('hook_img/hook8.png'), 0.6),
    # (cv2.imread('hook_img/hook9.png'), 0.6),
    # (cv2.imread('hook_img/hook10.png'), 0.6),
    (cv2.imread('hook_img/spark1.png'), 0.6), # 钓鱼点: 灰谷右上角 (98.1 29.2) 站在一个小浅滩上, 面朝东岸, 背景尽量固定, 这个位置效果可以
    # (cv2.imread('hook_img/spark1.png'), 0.6),  # 钓鱼点: 北风苔原 魔枢西边 (8.3, 53.9) ALT+Z后才可使用0.5
    # (cv2.imread('hook_img/spark2.png'), 0.6),
    # (cv2.imread('hook_img/spark3.png'), 0.7),
    # (cv2.imread('hook_img/spark4.png'), 0.7),
    # (cv2.imread('hook_img/spark5.png'), 0.7),
    (cv2.imread('hook_img/spark6.png'), 0.55),  # 钓鱼点: 风暴峭壁 (43.7,2.3) 面向深色的水域(向正东). 背景颜色要深, 使用的bobber3.png来定位鱼漂;    # 索拉查盆地 (17.2, 60.7) 配合 bobber4
]


# 这个等待鱼上钩, 然后右键点击
def try_click_hook():
    # 对屏幕截图
    screenshot = pyautogui.screenshot()
    screenshot = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2BGR)
    # 尝试在截图里找鱼是否上钩
    for (hook_image, confidence) in hook_images:
        if do_try_hook(screenshot, hook_image, confidence):
            return True
    return False


def do_try_hook(screenshot, hook_image, confidence):
    # result是一个二维数组
    # 这个二维数组中的每个元素表示在输入图像（screenshot）中的对应位置与模板图像（target_image）的匹配程度。
    result = cv2.matchTemplate(screenshot, hook_image, cv2.TM_CCOEFF_NORMED)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
    print(f"waiting: max_val={max_val}, max_loc={max_loc}")
    # max_val 是可信度, 这个比较高表示匹配度高, 有比较大可能是鱼漂上钩了
    if max_val > confidence:
        # 认为上钩的情况
        print()
        print("========= 鱼上钩了 =========")
        print(f"harvesting: max_val={max_val}, max_loc={max_loc}")
        # 随机等待 0.0~1.0秒, 防止系统检测
        random_number = np.random.rand()
        time.sleep(random_number)

        # 匹配度比较高的示例
        # min_val = -0.35622406005859375, max_val=0.45643624663352966, min_loc=(1005, 11), max_loc=(872, 353)
        # min_val = -0.35637786984443665, max_val=0.46077489852905273, min_loc=(1005, 11), max_loc=(881, 357)
        # min_val = -0.3535726070404053, max_val=0.424777626991272, min_loc=(997, 971), max_loc=(1088, 493)
        # 当匹配度比较低时候的数据示例
        # min_val = -0.35622039437294006, max_val=0.3448461592197418, min_loc=(1005, 11), max_loc=(1452, 203)
        # min_val = -0.3535724878311157, max_val=0.3937825858592987, min_loc=(997, 971), max_loc=(0, 227)

        # 收鱼
        # 根据水花的位置收鱼
        # click_spark(hook_image, max_loc)

        # 直接收鱼
        print("========= 收鱼 =========")
        locate_bobber.click_bobber()
        print()
        return True
    else:
        return False


def click_spark(hook_image, max_loc):
    # 这个是匹配图片的左上角, 这里一般点不着, 需要找到匹配图片的中间位置
    top_left = max_loc
    # 由于匹配的是水花, 所以点击水花的中间下面
    h, w = hook_image.shape[:2]
    bottom_right = (top_left[0] + w, top_left[1] + h)
    x = (top_left[0] + bottom_right[0]) // 2  # 中间
    y = top_left[1] + h
    print(x, y)
    # 点击匹配的图片中心点
    pyautogui.moveTo(x, y)  # 先移动过来, 再右击, 这样就不会让游戏误判导致拖拉屏幕了
    time.sleep(1)
    pyautogui.rightClick(x, y)


# 等待上钩一次
def waiting_hook_once():
    # 刚甩干不会有鱼, 等3秒, 钓鱼引导一共17秒
    time.sleep(3)
    start_time = time.time()
    print("========= 开始等鱼 =========")
    while True:
        time.sleep(0.1)
        # 1. 检查超时则终端
        current_time = time.time()
        elapsed_time = current_time - start_time
        if elapsed_time >= 14:
            break
        # 2. 每次检查间隔, 防止数据刷得太频繁
        # time.sleep(0.1)
        # 3. 尝试检查屏幕上的鱼漂有没有鱼上钩
        hooked = try_click_hook()
        if hooked:
            # 如果上钩了, 则结束方法
            return

    print("========= 没有鱼 =========")

# waiting_hook_once()
